/*
 * 	jQuery Dialog Manager 1.0
 * 
 * 	Represents jQuery Dialog Manager, which helps to open 
 * 	and use modal dialogs
 *  
 *	Author: Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 *	Date: 15/03/2013
 *
 *	Depends:
 *		jQuery,
 *		jQuery UI (dialog),
 *  
 *////-----------------------------------------------------------------------//

var dialogManager = {
    _stack: new Array(),
    init:  function(baseurl){
        this.getBaseUrl = function(){
            return baseurl; // private
        }
    },
    open: function(options){
        var defaults = {
            type: 'GET',
            modal: true,
            title: 'Modal Dialog',
            draggable: true,
            close: function(){
                $(this).empty().remove();
                console.log('dialogManager: dialog was closed and cleaned.');
            },
            open: function(event, ui){
                //dialogManager.showWaitDialog();
            },
            resize: function(event, ui) {
                // code here
            }
        }
        
        var settings = $.extend(true, {}, defaults, options)
        settings.title+=' - Modal Dialog';
        
        var $dialog = $('<div>').appendTo('body');
        $dialog.callback = settings.callback;
        $dialog.data = settings.data;
        
        console.log('dialogManager: open: url = '+this.getBaseUrl()+settings.url);
        
        $.ajax({
            url: this.getBaseUrl()+'/'+settings.url,
            data: settings.data,
            type: settings.type,
            dataType: 'html',
            success: function(data, textStatus, jqXHR) {
                $dialog.append(data);
                console.log('dialogManager: open: content was loaded!');
                $dialog.dialog(settings);
            },
            error: function(jqXHR, textStatus, errorThrown){
                Notifier.error(errorThrown, 'Error: '+jqXHR.status);
            }
        });

        this._addDialog($dialog);
        return $dialog;
    },
    _addDialog: function(dlg){
        if(this._stack==null) return;
        this._stack.push(dlg);
    },
    getDialog: function(index){
        if(this._stack.length==0) return null;
        if(index!=null) {
            return this._stack[index];
        } else {
            return this._stack[this._stack.length-1]; //the last
        }
    },
    _getDialog: function(){
        if(this._stack.length==0) return null;
        return this._stack.pop();
    },
    closeDialog: function(returnValue){
        var $dlg = this._getDialog();
        if($dlg==null) return;

        if($dlg.callback) $dlg.callback(returnValue);
        $dlg.dialog('close');
    },
    getCloseButton: function(options){
        return uic.element.button({
            onclick: function(){
                dialogManager.closeDialog();
                if(options.callback) options.callback();
            },
            id: options && options.id ? options.id : 'dialog-manager-cancel',
            title: options && options.title ? options.title : 'Cancel'
        })
    }
}